<?php
namespace humhub\modules\api\controllers;

use Yii;
use yii\rest\ActiveController;
use yii\web\UnauthorizedHttpException;
use yii\filters\VerbFilter;
use yii\filters\auth\QueryParamAuth;

use humhub\modules\api\models\ApiUser;
use humhub\modules\post\models\Post;

/**
 * Base controller
 */
class Base3Controller extends \humhub\modules\api\cccstream\ContentAddonController
{
    
    const MAX_ROWS = 1000;
    protected $client_id = '';

    /**
     * @inheritdoc
     */
    public function init(){
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        Yii::$app->user->enableSession = false;
        parent::init();
    }

    /**
     * @inheritdoc
     */
    public function beforeAction($action) {
        Yii::$app->request->parsers =  [
          'application/json' => 'yii\web\JsonParser',
        ];
        Yii::$app->request->enableCsrfValidation = false;
        if (!parent::beforeAction($action)) {
            return false;
        }

        $req = Yii::$app->request;
        parse_str($req->queryString);
        if (!isset($access_token)) {
            throw new UnauthorizedHttpException('Access unavailable without access_token.', 401);
        }
        if ($api = ApiUser::findIdentityByAccessToken($access_token)) {
            $this->client_id = $api->getClient();
            return true;
        }
        throw new UnauthorizedHttpException('You are requesting with an invalid credential.', 401);
    }
}
